package com.squareup.picasso;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.BitmapFactory;

import com.squareup.picasso.Picasso.LoadedFrom;

import java.io.IOException;

final class ResourceRequestHandler extends RequestHandler {
    private final Context context;

    ResourceRequestHandler(Context context) {
        this.context = context;
    }

    public final boolean canHandleRequest(Request result) {
        return result.resourceId != 0 || "android.resource".equals(result.uri.getScheme());
    }

    public final Result load(Request request) throws IOException {
        Resources resources = Utils.getResources(this.context, request);
        int resId = Utils.getResourceId(resources, request);
        BitmapFactory.Options options = ResourceRequestHandler.createBitmapOptions(request);
        if(ResourceRequestHandler.inJustDecodeBounds(options)) {
            BitmapFactory.decodeResource(resources, resId, options);
            ResourceRequestHandler.calculateInSampleSize(request.targetWidth, request.targetHeight, options, request);
        }

        return new Result(BitmapFactory.decodeResource(resources, resId, options), LoadedFrom.DISK);
    }
}

